<html>
<head> 
	<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-mobile/1.4.5/jquery.mobile.min.css">
    <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-mobile/1.4.5/jquery.mobile.min.js"></script>
    <script src="https://cdn.bootcss.com/require.js/2.3.6/require.min.js"></script>
    <title>热电阻温度计算</title> 

<script>
	function toRound(value){
		var value2;
		if (value > 1000){
			value2 = value.toFixed(0);
		}
		else if (value > 100){
			value2 = value.toFixed(1);
		}
		else if (value > 10){
			value2 = value.toFixed(2);
		}
		else if (value > 1){
			value2 = value.toFixed(3);
		}
		else{
			value2 = value.toFixed(4);
		}
		return value2
	}

	// PT100阻值计算温度
	function pt100_r2t(r){
		var A = 3.9083E-3;
		var B = -5.775E-7;
		var C = -4.183E-12;

		var t = 0.0
	    var t0 = (r / 100 - 1) / A ;
	    if(r >= 18.52 && r < 100)                                                     //-200℃  ~ 0℃
	    {
	        for(i = 0 ; i < 50 ; i ++)
	        {
	            t = t0 + 
	                (r - 100*(1 + A*t0 + B*t0*t0 - 100*C*t0*t0*t0 + C*t0*t0*t0*t0)) / 
	                (100 * (A + 2*B*t0 - 300*C*t0*t0 + 4*C*t0*t0*t0)) ;
	            if((t - t0) > -0.001 && (t - t0) < 0.001)
	                break;
	            else
	                t0 = t ;
	        }
	    }
	    else if(r >= 100 && r <= 390.481)                                             //0℃  ~ 850℃
	    {
	        for(i = 0 ; i < 50 ; i ++)
	        {
	            t = t0 + (r - 100*(1 + A*t0 + B*t0*t0)) / (100*(A + 2*B*t0)) ;
	            if((t - t0) > -0.001 && (t - t0) < 0.001)
	                break ;
	            else
	                t0 = t ;
	        }
	    }
	    else                                                                            // 超量程
	    {
	        if (r < 18.52)
	        {
	            t = -999;
	        }
	        else
	        {
	            t = -999;
	        }
	    }

	    return t;
	}

	// PT100温度计算阻值
	function pt100_t2r(t){
		var A = 3.9083E-3;
		var B = -5.775E-7;
		var C = -4.183E-12;
		var R0 = 100.0;

		var r = 0.0;
		if (t >= 0.0) {
			r = R0 * (1 + A * t + B * t * t)
		}
		else {
			r = R0 * [1 + A * t + B * t * t + C * (t - 100) * t * t * t]
		}
		return r
	}

	// 计算流速
	function calcTemperature(){
		// 获取传感器类型
		var sensor = $('[name="sensor"]:checked').val();

		var resistance = document.getElementById('resistance').value;
		var temperature = ''
		message = ''
		if (sensor == 'pt100') {
			temperature = pt100_r2t(resistance)
		}
		else if (sensor == 'pt1000'){
			temperature = pt100_r2t(resistance / 10)
		}

		if (temperature != -999) {
			temperature = temperature.toFixed(2)
		}
		else {
			temperature = ''
			message = "电阻值超出范围"
		}
		document.getElementById('temperature').value = temperature
		document.getElementById('message').innerHTML = message
	}

	// 计算阻值
	function calcResistance(){
		// 获取传感器类型
		var sensor = $('[name="sensor"]:checked').val();
		var temperature = document.getElementById('temperature').value;
		var resistance = ''
		var message = ''

		if (temperature == '') {
			message = '请输入温度值！'
		}
		else if (temperature < -200 || temperature > 850) {
			message = '温度值超出范围（-200 ~ 850℃）！'
		}
		else {
			if (sensor == 'pt100') {
				resistance = pt100_t2r(temperature).toFixed(2)
			}
			else if (sensor == 'pt1000'){
				resistance = (pt100_t2r(temperature) * 10).toFixed(2)
			}
		}

        document.getElementById('resistance').value = resistance
		document.getElementById('message').innerHTML = message

	}
</script>
<style>
.ui-input{
  float:left;
  width: 150px; 
}
.ui-input>div{
    display: inline-block;
    line-height: 26px;
    height: 26px;
}

.ui-lable{
   float:left;
   line-height:36px; 
   width: 50px; 
}

.ui-lable.ui-lable-left{
   line-height:36px;	
}
</style>

</head>
<body>
    <div data-role="page" id="pageone">
		<div data-role="header" data-position="fixed">
			<h1 id="header-device-detail">热电阻温度计算</h1>
			<a href="javascript:document.location='./flow.html'" class="ui-btn ui-btn-right ui-alt-icon ui-nodisc-icon ui-corner-all ui-btn-icon-notext ui-icon-back"></a>
		</div>

		<div data-role="main" class="ui-content">
		
		<form action="">
			<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
				<legend><b>热电阻类型</b>：</legend>
				<label for="pt100">PT100</label>
				<input type="radio" name="sensor" id="pt100" onchange="calcResistance()" value="pt100" checked>
				<label for="pt1000">PT1000</label>
				<input type="radio" name="sensor" id="pt1000" onchange="calcResistance()" value="pt1000">
			</fieldset>
			<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
				<legend><b>单位</b>：</legend>
				<label for="celsius">℃</label>
				<input type="radio" name="degree" id="celsius" onchange="calcResistance()" value="celsius" checked>
				<!-- <label for="fahrenheit">℉</label> -->
				<!-- <input type="radio" name="degree" id="fahrenheit" onchange="calcResistance()" value="fahrenheit"> -->
			</fieldset>

			<li data-role="fieldcontain">
				<div class="ui-grid-a">
					<div class="ui-lable ui-lable-left">
					    <label><b>温度：</b></label>
				    </div>
					<div class="ui-input">
					    <input type="text" name="temperature" id="temperature" data-mini="true" onchange="calcResistance()">
					</div>
					<div class="ui-lable">
					    <label ></label>
				    </div>
				</div>
				<div class="ui-grid-a">
					<div class="ui-lable ui-lable-left">
					    <label><b>阻值：</b></label>
				    </div>
					<div class="ui-input">
					    <input type="text" name="resistance" id="resistance" data-mini="true" onchange="calcTemperature()">
					</div>
					<div class="ui-lable">
					    <label >Ω</label>
				    </div>
				</div>
			</li>
		</form>
			<li data-role="fieldcontain">
				<div id="message">
					<p>* 输入温度值或电阻值进行计算</p>
					<p>* 切换类型时，用温度计算阻值</p>
				</div>
			</li>
	    </div>
	</div>
</body>
</html>